package com.koron.ticket.fake.operation;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.ticket.fake.operation.bean.OperationFormworkDto;
import com.koron.ticket.fake.operation.bean.OperationFormworkQuery;
import com.koron.ticket.fake.operation.mapper.OperationFormworkMapper;
import com.koron.ticket.fake.operation.mapper.OperationFormworkStepMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: rocky
 * @Date: 2022/1/5 14:32
 */
@Slf4j
@RestController
@RequestMapping("fake/OperationFake")
@Api(tags = "操作票-样票")
public class OperationFakeController {

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "操作票样票列表")
    @PostMapping("/queryList")
    public Response queryList(@RequestBody OperationFormworkQuery query) {
        PageHelper.startPage(query.getPage(), query.getPageSize(), query.getOrderBy());
        PageInfo pageInfo = ADOConnection.runTask(getCurrDs(), factory -> {
            List<OperationFormworkQuery> vos = factory.getMapper(OperationFormworkMapper.class).queryAllByGroupAndTitle(query);
            return new PageInfo(vos);
        }, PageInfo.class);
        return Response.ok(pageInfo);
    }

    @ApiOperation(value = "样票删除")
    @GetMapping("/delete")
    public Response delete(@RequestParam("id") String id) {
        ADOConnection.runTask(getCurrDs(), factory -> {
            factory.getMapper(OperationFormworkMapper.class).deleteByPrimaryKey(id);
            factory.getMapper(OperationFormworkStepMapper.class).deleteByOperationId(id);
            return true;
        }, Boolean.class);
        return Response.ok("成功");
    }

    @PostMapping("/fakeSave")
    @ApiOperation(value = "保存")
    public Response fakeSave(@RequestBody OperationFormworkDto dto) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), new OperationFakeService(), "fakeSave", String.class, dto));
    }

    @GetMapping("/details")
    @ApiOperation(value = "样票详情")
    public Response details(@RequestParam("id") String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), new OperationFakeService(), "details", OperationFormworkDto.class, id));
    }

}
